﻿using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Project.Domain.Entity;
using Project.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace Project.Application.Users
{
    public class UserAppService : Repository<User>, IUserAppService
    {

        private readonly TokenDto _tokenManagement;

        public UserAppService(IOptions<TokenDto> tokenManagement)
        {
            _tokenManagement = tokenManagement.Value;
        }
        public IEnumerable<string> Login(UserLoginRequestDto request)
        {
            string token = string.Empty;

            //登录逻辑

            //登录逻辑

            var claims = new[]
            {
                new Claim(ClaimTypes.Name,request.UserName)
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenManagement.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(_tokenManagement.Issuer, _tokenManagement.Audience, claims, expires: DateTime.Now.AddMinutes(_tokenManagement.AccessExpiration), signingCredentials: credentials);
            token = new JwtSecurityTokenHandler().WriteToken(jwtToken);
            return new string[] { "JWT", token };
        }
    }
}
